package lg.minecraft.plugin.worldprotect.command;

import lg.minecraft.plugin.library.command.CommandContext;
import lg.minecraft.plugin.library.command.CommandType;
import lg.minecraft.plugin.library.command.SubCommand;
import lg.minecraft.plugin.worldprotect.gui.MainGUI;
import lg.minecraft.plugin.worldprotect.gui.SettingsGUI;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author langle__
 * @version 1.0
 */
public class OpenCommand extends SubCommand {

    public OpenCommand() {
        super("open", "o");
    }

    @Override
    public String getDescription() {
        return "世界编辑";
    }

    @Override
    public String getPermission() {
        return "LGWorldProtect.command.edit";
    }

    @Override
    public CommandType getCommandType() {
        return CommandType.PLAYER;
    }

    @Override
    public void onCommand(CommandContext context) {
        Player player = context.getPlayer();
        if (context.getArgLength() == 0) {
            MainGUI mainGUI = new MainGUI();
            mainGUI.openInventory(player);
            return;
        }
        String worldName = context.getArg(0);
        World world = Bukkit.getWorld(worldName);
        if (world == null) {
            player.sendMessage("§c世界不存在");
            return;
        }
        SettingsGUI mainGUI = new SettingsGUI(world);
        mainGUI.openInventory(player);
    }

    @Override
    public List<String> onTabComplete(CommandContext context) {
        if (context.getArgLength() == 1) {
            return Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList());
        }
        return null;
    }

}
